﻿namespace Magic.Redis;

/// <summary>
/// 提供Redis客户端操作类（基于CsRedisCore实现）
/// </summary>
public sealed partial class CsRedisCoreClient : IRedisClient
{
	/// <summary>
	/// HashDelete.
	/// </summary>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public bool HashDelete(string key, string field)
	{
		try
		{
			RedisHelper.HDel(key, field);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashDelete.key={key}&field={field}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashDeleteAsync.
	/// </summary>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public async Task<bool> HashDeleteAsync(string key, string field)
	{
		try
		{
			await RedisHelper.HDelAsync(key, field);
			return true;
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashDeleteAsync.key={key}&field={field}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashExists.
	/// </summary>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public bool HashExists(string key, string field)
	{
		try
		{
			return RedisHelper.HExists(key, field);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashExists.key={key}&field={field}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashExistsAsync.
	/// </summary>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public Task<bool> HashExistsAsync(string key, string field)
	{
		try
		{
			return RedisHelper.HExistsAsync(key, field);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashExistsAsync.key={key}&field={field}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashGet.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public T HashGet<T>(string key, string field)
	{
		try
		{
			return RedisHelper.HGet<T>(key, field);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashGet.key={key}&field={field}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashGetAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <returns></returns>
	public Task<T> HashGetAsync<T>(string key, string field)
	{
		try
		{
			return RedisHelper.HGetAsync<T>(key, field);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashGetAsync.key={key}&field={field}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashSet.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public bool HashSet<T>(string key, string field, T value)
	{
		try
		{
			return RedisHelper.HSet(key, field, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashSet.key={key}&field={field}&value={Serialize(value)}", ex);
			return default;
		}
	}

	/// <summary>
	/// HashSetAsync.
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="key"></param>
	/// <param name="field"></param>
	/// <param name="value"></param>
	/// <returns></returns>
	public Task<bool> HashSetAsync<T>(string key, string field, T value)
	{
		try
		{
			return RedisHelper.HSetAsync(key, field, value);
		}
		catch (Exception ex)
		{
			Faild($"An exception occurs on HashSetAsync.key={key}&field={field}&value={Serialize(value)}", ex);
			return default;
		}
	}
}
